package com.qingcheng.service.impl;

import com.alibaba.druid.sql.visitor.functions.If;
import com.qingcheng.service.goods.BrandService;
import com.qingcheng.service.goods.CategoryService;
import com.qingcheng.service.goods.SkuService;
import com.qingcheng.service.goods.SpecService;
import com.qingcheng.util.CacheKey;
import org.junit.Test;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 工程启动，先初始化加载
 */
@Component
public class Init implements InitializingBean {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SkuService skuService;

    @Autowired
    private BrandService brandService;

    @Autowired
    private SpecService specService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("---缓存预热---");
        categoryService.saveCategoryTreeToRedis();
        skuService.saveAllPriceToRedis();

        //这个不仅要缓存预热，还要定时缓存更新
        if(!redisTemplate.hasKey(CacheKey.CATEGORY_BRAND)){ //先检测是否有数据,没有再执行
            System.out.println("保存到缓存中");
            brandService.saveBrandByCategoryToRedis();
        }else{
//            System.out.println("已经存在了品牌");
        }
        if(redisTemplate.hasKey(CacheKey.CATEGORY_SPEC)) { //先检测是否有数据,没有再执行
            specService.saveSpecByCategoryToRedis();
        }else{
//            System.out.println("已经存在了规格");
        }
    }

}
